package com.eam.eventhandler;

import com.eam.data.EamMaintainPlanItems;
import org.hibernate.Query;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.base.model.Property;
import org.openbravo.client.kernel.event.EntityDeleteEvent;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.database.SessionInfo;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.service.db.DalConnectionProvider;
import javax.enterprise.event.Observes;

public class MaintainDetailsEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EamMaintainPlanItems.ENTITY_NAME) };

	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}

	public void onUpdate(@Observes EntityUpdateEvent event) throws Throwable {
		if (!isValidEvent(event)) {
			return;
		}
//
//		String tabId = SessionInfo.getProcessId();
//
//		if (tabId == null) {
//			return;
//		}
//
//		// 获取计划
//		EamMaintainPlanItems details = (EamMaintainPlanItems) event.getTargetInstance();
//		EamMaintainPlanItems old_item = null;
//        
//		String o_id = details.getId();
//		String hqlOld = "select empd from " + EamMaintainPlanItems.ENTITY_NAME + " empd where empd.id = :o_id";
//		Query queryOld = OBDal.getInstance().getSession().createQuery(hqlOld);
//		queryOld.setString("o_id", o_id);
//		
//		old_item = (EamMaintainPlanItems) queryOld.uniqueResult();
//		
//		// 获取item
//		String i_id = details.getEamMaintainPlanFacs().getId();
//		String d_id = "";
//
//		if (tabId.equalsIgnoreCase("7C35BB072E4A4E33958AB1662A55E5FB")) {
//			if (!old_item.getEamRepairItem().getId().equalsIgnoreCase(details.getEamRepairItem().getId())) {
//				d_id = details.getEamRepairItem().getId();
//				String hql = "select empd from " + EamMaintainPlanItems.ENTITY_NAME + " empd where empd.emesMaintainPlanItem.id =:i_id and empd.emesRepairItem.id =:d_id ";
//				Query query = OBDal.getInstance().getSession().createQuery(hql);
//				query.setString("i_id", i_id);
//				query.setString("d_id", d_id);
//				old_item = (EamMaintainPlanItems) query.uniqueResult();
//				if (old_item != null) {
//					String language = OBContext.getOBContext().getLanguage().getLanguage();
//					ConnectionProvider conn = new DalConnectionProvider(false);
//					throw new OBException(Utility.messageBD(conn, (details.getEamMaintainItem() != null ? details
//									.getEamMaintainItem().getName() : details.getEamRepairItem().getName())
//									+ "已经添加，请仔细检查", language));
//				}
//				return;
//			}
//		} else {
//			if (!old_item.getEamMaintainItem().getId().equalsIgnoreCase(details.getEamMaintainItem().getId())) {
//				d_id = details.getEamMaintainItem().getId();
//				String hql = "select empd from " + EamMaintainPlanItems.ENTITY_NAME + " empd where empd.emesMaintainPlanItem.id =:i_id and empd.emesMaintain.id =:d_id ";
//				Query query = OBDal.getInstance().getSession().createQuery(hql);
//				query.setString("i_id", i_id);
//				query.setString("d_id", d_id);
//				old_item = (EamMaintainPlanItems) query.uniqueResult();
//				if (old_item != null) {
//					String language = OBContext.getOBContext().getLanguage().getLanguage();
//					ConnectionProvider conn = new DalConnectionProvider(false);
//					throw new OBException(Utility.messageBD(conn, (details.getEamMaintainItem() != null ? details
//									.getEamMaintainItem().getName() : details.getEamRepairItem().getName())
//									+ "已经添加，请仔细检查", language));
//				}
//				return;
//			}
//		}

	}
	
	//新增
	public void onSave(@Observes EntityNewEvent event) {
		if (!isValidEvent(event)) {
			return;
		}

		String tabId = SessionInfo.getProcessId();

		if (tabId == null) {
			return;
		}

		// 获取计划
		EamMaintainPlanItems details = (EamMaintainPlanItems) event.getTargetInstance();
		// 将要更改的实体类
		final Entity detailsEntity = ModelProvider.getInstance().getEntity(EamMaintainPlanItems.ENTITY_NAME);
		final Property typeProperty = detailsEntity.getProperty(EamMaintainPlanItems.PROPERTY_TYPE);

		EamMaintainPlanItems old_item = null;

		// 获取item
		String i_id = details.getEamMaintainPlanFacs().getId();
		String d_id = "";
		String status = "";
		// 7C35BB072E4A4E33958AB1662A55E5FB为维修页签
		if (tabId.equalsIgnoreCase("7C35BB072E4A4E33958AB1662A55E5FB")||tabId.equalsIgnoreCase("11C049757AA14623931F7C06B06AF5A4")||tabId.equalsIgnoreCase("66001EFC18E340C2BDAF9DDD27FE132D")) {
			d_id = details.getEamRepairItem().getId();
			String hql = "select empd from " + EamMaintainPlanItems.ENTITY_NAME + " empd where empd.emesMaintainPlanItem.id =:i_id and empd.emesRepairItem.id =:d_id ";
			Query query = OBDal.getInstance().getSession().createQuery(hql);
			query.setString("i_id", i_id);
			query.setString("d_id", d_id);
			old_item = (EamMaintainPlanItems) query.uniqueResult();
		} else {
			d_id = details.getEamMaintainItem().getId();
			status = details.getEamMaintainPlanFacs().getStatus(); //设备状态
			
			if (status.equals("OK")) {
				String language = OBContext.getOBContext().getLanguage().getLanguage();
				ConnectionProvider conn = new DalConnectionProvider(false);
				throw new OBException(Utility.messageBD(conn, details.getEamMaintainPlanFacs().getEamFacilities().getName()+ "设备已点检完，无法新增保养项目", language));
			}else{
				String hql = "select empd from " + EamMaintainPlanItems.ENTITY_NAME + " empd where empd.emesMaintainPlanItem.id =:i_id and empd.emesMaintain.id =:d_id ";
				Query query = OBDal.getInstance().getSession().createQuery(hql);
				query.setString("i_id", i_id);
				query.setString("d_id", d_id);
				old_item = (EamMaintainPlanItems) query.uniqueResult();
			}
			
			
		}

		
		
		if (old_item != null) {
			String language = OBContext.getOBContext().getLanguage().getLanguage();
			ConnectionProvider conn = new DalConnectionProvider(false);
			throw new OBException(Utility.messageBD(conn, (details.getEamMaintainItem() != null ? details
							.getEamMaintainItem().getName() : details.getEamRepairItem().getName())
							+ "已经添加，请仔细检查", language));
		}

		if (tabId.equalsIgnoreCase("7C35BB072E4A4E33958AB1662A55E5FB")||tabId.equalsIgnoreCase("11C049757AA14623931F7C06B06AF5A4")||tabId.equalsIgnoreCase("66001EFC18E340C2BDAF9DDD27FE132D")) {
			event.setCurrentState(typeProperty, "REPAIR"); // 维修项目录入
		} else {
			event.setCurrentState(typeProperty, "MAINTAIN"); // 保养项目录入
		}
		
	}

	public void onDelete(@Observes EntityDeleteEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
	}

}
	